package com.entity.implement;

import java.util.Date;
import java.util.List;

import com.entity.myInterface.RoleRepositoryCustom;
import com.entity.paging.RoleSearchCondition;
import com.shiro.entity.Role;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;


@Repository
public class RoleRepositoryImpl implements RoleRepositoryCustom {
    private static Logger logger = LoggerFactory
            .getLogger(RoleRepositoryImpl.class);
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public Page<Role> search(int pageNo, int pageSize, String condition, String app) {

        Query query = new Query();

        Criteria criteria = Criteria.where("delflag").is(false).and("app").is(app);
        if (null != condition && !"".equals(condition)){
            criteria.and("name").regex(condition);
        }

        Pageable pageable = PageRequest.of(pageNo - 1, pageSize);

        query.addCriteria(criteria);
        long total = mongoTemplate.count(query, Role.class);

        query.skip(pageable.getOffset());
        query.limit(pageable.getPageSize());
        query.with(new Sort(Sort.Direction.ASC,"name"));
        List<Role> courses = mongoTemplate.find(query,Role.class);

        if (courses == null) {
            return null;
        }

        PageImpl<Role> page = new PageImpl<Role>(courses,
                pageable, total);

        return page;
    }



    @Override
    public List<Role> findAll(String app) {
        Query query = new Query();

        Criteria criteria = Criteria.where("delflag").is(false).and("app").is(app);
        query.addCriteria(criteria);

        List<Role> result = mongoTemplate.find(query,Role.class);

        return result;
    }

    @Override
    public Page<Role> findByPage(RoleSearchCondition rsc){

        Query query = new Query();
        Criteria criteria = Criteria.where("delflag").is(false);

        if(rsc.getName() != null){
            criteria.and("name").is(rsc.getName());
        }
        if(rsc.getApp() !=null){
            criteria.and("app").is(rsc.getApp());
        }
        if(rsc.getCode() !=null){
            criteria.and("code").is(rsc.getCode());
        }
        String pattern = "^.*%s.*$";
        if (rsc.getSearcword() != null && !rsc.getSearcword().equals("")){
            criteria.orOperator(
                    Criteria.where("name").regex(String.format(pattern, rsc.getSearcword())),
                    Criteria.where("app").regex(String.format(pattern, rsc.getSearcword())),
                    Criteria.where("code").regex(String.format(pattern, rsc.getSearcword()))
            );
        }

        Pageable pageable =  PageRequest.of(rsc.getPageNo() - 1, rsc.getPageSize());
        query.addCriteria(criteria);
        long total = mongoTemplate.count(query, Role.class);
        query.skip(pageable.getOffset());
        query.limit(pageable.getPageSize());
        List<Role> roles = mongoTemplate.find(query, Role.class);

        if(roles == null){
            logger.info("查询失败");
            return null;
        }

        PageImpl<Role> page = new PageImpl<Role>(roles, pageable, total);

        return page;
    }

    @Override
    public Role findByCode(String code){
        Query query=new Query();
        query.addCriteria(Criteria.where("code").is(code)
                .and("delflag").is(false));
        return mongoTemplate.findOne(query, Role.class);
    }

    @Override
    public void update(Role role,String op){
        Query query=new Query();
        query.addCriteria(Criteria.where("id").is(role.getId()));

        Update update = new Update();
        update.set("modifier", op);
        update.set("modifyDate", new Date());

        if(StringUtils.isNotBlank(role.getApp())){
            update.set("app", role.getApp());
        }
        if(StringUtils.isNotBlank(role.getCode())){
            update.set("code", role.getCode());
        }
        if(StringUtils.isNotBlank(role.getName())){
            update.set("name", role.getName());
        }
        if(role.getPermissions()!=null){
            update.set("permissions", role.getPermissions());
        }
        mongoTemplate.updateFirst(query, update, Role.class);

    }
}